package org.robotlegs.utilities.modular.mvcs
{
	import flash.system.ApplicationDomain;
	
	import org.robotlegs.base.SignalCommandMap;
	import org.robotlegs.core.IInjector;
	import org.robotlegs.core.ISignalCommandMap;
	import org.robotlegs.utilities.modular.base.StarlingModuleSignalCommandMap;
	import org.robotlegs.utilities.modular.core.IStarlingModuleSignalCommandMap;
	import org.robotlegs.utilities.modular.core.IStarlingModuleSignalContext;
	
	import starling.display.DisplayObjectContainer;

    public class StarlingModuleSignalContext extends FixedStarlingModuleContext implements IStarlingModuleSignalContext
	{
		
		protected var _moduleSignalCommandMap:IStarlingModuleSignalCommandMap;
		protected var _signalCommandMap:ISignalCommandMap;
		
		                                                    
		public function StarlingModuleSignalContext(contextView:DisplayObjectContainer = null, autoStartup:Boolean = true, parentInjector:IInjector = null, applicationDomain:ApplicationDomain = null)
		{
			super(contextView, autoStartup, parentInjector, applicationDomain);
		}
		
		
		override public function dispose():void
		{
			super.dispose();
			signalCommandMap = null;
			moduleSignalCommandMap = null;
		}
		
		
		public function get moduleSignalCommandMap():IStarlingModuleSignalCommandMap
		{
			return _moduleSignalCommandMap;
		}
		
		
		public function set moduleSignalCommandMap(value:IStarlingModuleSignalCommandMap):void
		{
			_moduleSignalCommandMap = value;
		}
		
		
		public function get signalCommandMap():ISignalCommandMap
		{
			return _signalCommandMap || (_signalCommandMap = new SignalCommandMap(injector.createChild(injector.applicationDomain)));
		}
		
		
		public function set signalCommandMap(value:ISignalCommandMap):void
		{
			_signalCommandMap = value; 
		}
		
		
		protected function initializeModuleSignalCommandMap():void
		{
			if (injector.hasMapping(IStarlingModuleSignalCommandMap))
			{
				moduleSignalCommandMap = injector.getInstance(IStarlingModuleSignalCommandMap);
			}
			else
			{
				moduleSignalCommandMap = new StarlingModuleSignalCommandMap(injector);
				injector.mapValue(IStarlingModuleSignalCommandMap, moduleSignalCommandMap);
			}
		}
		
		
		override protected function mapInjections():void
		{
			super.mapInjections();
			initializeModuleSignalCommandMap();
			injector.mapValue(ISignalCommandMap, signalCommandMap);
		}
	}
}